
package org.testfire.realm.control.user;

import org.testfire.realm.control.exception.UserNotFoundException;


/**
 * A bean for 'Confirming' a new m_user; Handles the data returned when a m_user followes the
 * link sent to them in the confirmation e-mail.
 *
 * Requires a managed bean tag in faces-config:
 *  <managed-bean>
		<managed-bean-name>ConfirmUserBean</managed-bean-name>
		<managed-bean-class>org.testfire.realm.control.m_user.ConfirmUserBean</managed-bean-class>
		<managed-bean-scope>request</managed-bean-scope>
	</managed-bean>
 * @author Geoff M. Granum
 * @version 1.0
 */
public class ConfirmUserBean extends UserBean {

    /** The m_user who is attempting to confirm their account.*/
    private String userName ="";

    /** The confirmation code which will be sent to the m_user. */
    private String confirmCode = "";


	/**
	 * Default construtor.
	 */
    public ConfirmUserBean() {
        super();
    }

    /**
     * Check that the value passed to us is indeed the same as the value we persisted to the
     * m_user's DB record. Update the m_user as confirmed if so, or fail if not.
     * @return Nav Case: nav_confirm_success or nav_confirm_fail
     */
    public String confirmUser() {
        boolean success = false;
        try {
            m_user = dbao().getUser(this.userName);
            success = m_user != null && m_user.getConfirm_code().equals(this.confirmCode);
            if (success) {
                m_user.setVerified(1);
                // Pass 'false' because don't need to test the m_user name, we have it,
                // and it is not on the external context to be tested.
                updateUserEntity(false);
            }
        } catch (Exception e) {
            e.printStackTrace();
            success = false;
        } catch (UserNotFoundException e)
        {
	        e.printStackTrace();
	        success = false;
        }
	    return success ? "nav_confirm_success" : "nav_confirm_fail";
    }


    /**
     * Get the confirmation code used to track this m_user's registration. This value will be sent to the m_user
     * in an e-mail, as part of a link, which will allow us to confirm that the m_user is, in fact, not a bot.
     * @return the confirmation code used to track this m_user's registration
     */
    public String getConfirmCode() {
        return confirmCode;
    }

    /**
     * Set the confimationCode used to track this m_user's registration.
     * @param confirmCode the confirmation code used to track this m_user's registration
     */
    public void setConfirmCode(String confirmCode) {
        this.confirmCode = confirmCode;
    }


    /**
     * Get the m_user name which is being confirmed.
     * @return the m_user name which is being confirmed.
     */
    public String getUserName() {
        return userName;
    }

    /**
     * Set the m_user name which is being confirmed.
     * @param userName the m_user name which is being confirmed.
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }
}
